local xiaheng = fk.CreateSkill{
  name = "rmt__xiaheng",
  tags = { Skill.Compulsory }
}

xiaheng:addEffect(fk.EventPhaseStart, {
  anim_type = "offensive",
  dynamic_desc = function(self, player)
    local tail = ""
    if player:getMark(xiaheng.name) ~= 1 then
      tail = Fk:translate("rmt__xiaheng_to_delete")
    end
    return "rmt__xiaheng_inner:" .. tail
  end,
  events = {},
  can_trigger = function (self, event, target, player, data)
    if player:hasSkill(xiaheng.name) and player == target then
      return player.phase == Player.Play
    end
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    local playerA, playerB ---@type ServerPlayer, ServerPlayer
    local tos = table.filter(room:getAlivePlayers(false), function (p)
      return not p:isNude()
    end)
    if #tos > 0 then
      playerA = room:askToChoosePlayers(player,{
        targets = tos,
        min_num = 1,
        max_num = 1,
        prompt = "#rmt__xiaheng-choose",
        skill_name = xiaheng.name,
        cancelable = false
      })[1]
      room:addTableMarkIfNeed(player, "rmt__xiaheng_chosen", playerA.id)
      local num = math.min(2, #playerA:getCardIds("he"))
      local cards = room:askToChooseCards(player, {
        target = playerA,
        min = num,
        max = num,
        flag = "he",
        skill_name = xiaheng.name,
        prompt = "#rmt__xiaheng-discard:"..playerA.id
      })
      room:throwCard(cards, xiaheng.name, playerA, player)
    end

    tos = room:getAlivePlayers(false)
    if #tos > 0 then
      playerB = room:askToChoosePlayers(player, {
        targets = tos,
        min_num = 1,
        max_num = 1,
        prompt = "#rmt__xiaheng-damage",
        skill_name = xiaheng.name,
        cancelable = false
      })[1]
      room:addTableMarkIfNeed(player, "rmt__xiaheng_chosen", playerB.id)
      room:damage{
        damage = 1,
        to = playerB,
        from = player,
        skillName = xiaheng.name,
      }
    end

    if playerA and playerB and player:getMark(xiaheng.name) == 0 then
      if not table.contains({playerA, playerB}, player) then
        room:changeMaxHp(player, -1)
      end
      if playerA == playerB then
        room:loseHp(player, 1, xiaheng.name)
      end
    end

    if player:getMark(xiaheng.name) == 0 and #player:getTableMark("rmt__xiaheng_chosen") == 3 then
      room:doAnimate("InvokeUltSkill", {
        name = xiaheng.name,
        player = player.id,
        deputy = player.deputyGeneral and player.deputyGeneral ~= "" and table.contains(Fk.generals[player.deputyGeneral]:getSkillNameList(true), xiaheng.name),
      })
      room:delay(2000)
      room:setPlayerMark(player, xiaheng.name, 1)
    end
  end,
})

xiaheng:addTest(function(room, me)
  FkTest.RunAddSkills(me, xiaheng.name)
  local comp2, comp3 = room.players[2], room.players[3]

  --- test1: 选择me和comp2
  --- 期望:  不减体力和体力上限
  FkTest.setNextReplies(me, {
    FkTest.replyChoosePlayer({ me }),
    FkTest.replyChoosePlayer({ comp2 })
  })

  FkTest.runInRoom(function ()
    me:gainAnExtraTurn(false, nil, {Player.Draw, Player.Play})
  end)

  lu.assertEquals(me.hp, 4)
  lu.assertEquals(me.maxHp, 4)

  --- test2: 选择comp3和comp3
  --- 期望:  减1点体力和1点体力上限，技能被修改
  FkTest.setNextReplies(me, {
    FkTest.replyChoosePlayer({ comp3 }),
    FkTest.replyChoosePlayer({ comp3 })
  })

  FkTest.runInRoom(function ()
    comp3:drawCards(2)
    me:gainAnExtraTurn(false, nil, {Player.Play})
  end)

  lu.assertEquals(me.maxHp, 3)
  lu.assertEquals(me.hp, 2)

  lu.assertIsNil(Fk:getDescription(xiaheng.name, nil, me):find(Fk:translate("rmt__xiaheng_to_delete")))

  --- test2: 选择comp3和comp3
  --- 期望:  不再扣减体力上限或体力值
  FkTest.setNextReplies(me, {
    FkTest.replyChoosePlayer({ comp3 }),
    FkTest.replyChoosePlayer({ comp3 })
  })

  FkTest.runInRoom(function ()
    comp3:drawCards(2)
    me:gainAnExtraTurn(false, nil, {Player.Play})
  end)

  lu.assertEquals(me.maxHp, 3)
  lu.assertEquals(me.hp, 2)
end)

Fk:loadTranslationTable{

  ["rmt__xiaheng"] = "侠横",
  [":rmt__xiaheng"] = "锁定技，出牌阶段开始时，你弃置一名角色两张牌（不足则全弃），再对一名角色造成1点伤害。"
  .."【若两名角色，均不为你，你减1点体力上限；为同一名角色，你失去1点体力；】"
  .."然后若以此法选择包括你在内三名不同的角色，你删除【】中的内容。",
  [":rmt__xiaheng_inner"] = "锁定技，出牌阶段开始时，你弃置一名角色两张牌（不足则全弃），再对一名角色造成1点伤害。{1}",
  ["rmt__xiaheng_to_delete"] = "【若两名角色，均不为你，你减1点体力上限；为同一名角色，你失去1点体力；】然后若以此法选择包括你在内三名不同的角色，你删除【】中的内容。",

  ["#rmt__xiaheng-choose"] = "侠横：弃置一名角色两张牌（不足则全弃）",
  ["#rmt__xiaheng-discard"] = "侠横：弃置 %src 两张牌（不足则全弃）",
  ["#rmt__xiaheng-damage"] = "侠横：对一名角色造成1点伤害",
}

return xiaheng
